In [44]:
import pandas as pd
%matplotlib inline
In [45]:
# load the raw data
df = pd.read_csv('../Data/shaneiphone_exp2.csv')
In [46]:
# plot gravity signal
df[['motionGravityX', 'motionGravityY', 'motionGravityZ']].plot()
Out[46]:
In [47]:
# plot gyroscope signal [indices 7000 to 10000 include a series of sharp turns in the Censio parking lot]
df[7000:10000][['gyroRotationX', 'gyroRotationY', 'gyroRotationZ']].plot()
Out[47]:
In [48]:
from quatrotate import qv_mult # this routine implements rotation via quaternion multiplication
import numpy as np
def getrot(quatern, vector):
rotatedvector = []
for i in range(vector.shape[0]):
rotatedvector.append(qv_mult(tuple(quatern[i, :]),
tuple(vector[i, :])))
return np.array(rotatedvector)
In [49]:
def rotate(df):
""" Generate rdf, a rotated version of df where the z-axis is aligned
with gravity. """
varlist = ['accelerometerAcceleration', 'motionUserAcceleration',
'motionGravity', 'motionMagneticField', 'gyroRotation']
quaternion = df[['motionQuaternionW', 'motionQuaternionX',
'motionQuaternionY', 'motionQuaternionZ']].values
for ivar in varlist:
print("..." + ivar)
xyzlist = [ivar + 'X', ivar + 'Y', ivar + 'Z']
xyz = df[xyzlist].values
xyz_rotated = getrot(quaternion, xyz)
df[ivar + 'X'] = xyz_rotated[:, 0]
df[ivar + 'Y'] = xyz_rotated[:, 1]
df[ivar + 'Z'] = xyz_rotated[:, 2]
In [50]:
# rotate does an in-place rotation
rdf = df.copy()
rotate(rdf)
In [51]:
rdf[['motionGravityX', 'motionGravityY', 'motionGravityZ']].plot()
Out[51]:
In [52]:
# indices 7000 to 10000 include a series of sharp turns in the Censio parking lot
rdf[7000:10000][['gyroRotationX', 'gyroRotationY', 'gyroRotationZ']].plot()
Out[52]:
In [ ]: